package com.diver.flinkdemo.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.diver.flinkdemo.entity.xxt.Depart;
import com.diver.flinkdemo.entity.xxt.PersonDepart;
import com.diver.flinkdemo.mapper.mysqlMapper.PersonDepartMapper;
import com.diver.flinkdemo.mapper.mysqlMapper.PersonTerminalInfoMapper;
import com.diver.flinkdemo.service.IPersonDepartService;
import com.diver.flinkdemo.utils.DynamicDataSourceUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;

/**
 * @Description: 人员 组织/机构表
 * @Author: jeecg-boot
 * @Date: 2022-05-10
 * @Version: V1.0
 */
@Slf4j
public class PersonDepartServiceImpl extends ServiceImpl<PersonDepartMapper, PersonDepart> implements IPersonDepartService {
    private PersonDepartMapper mapper;
    private SqlSession session = null;

    @Override
    public Depart getBindMasterDepart(String personId) {
        try {
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
            mapper = session.getMapper(PersonDepartMapper.class);
            return mapper.getBindMasterDepart(personId);
        } finally {
            endOfSession(session);
        }
    }

    @Override
    public Depart getDepartByTerminalId(String terminalId) {
        try {
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
            mapper = session.getMapper(PersonDepartMapper.class);
            return mapper.getDepartByTerminalId(terminalId);
        } finally {
            endOfSession(session);
        }
    }

    public void endOfSession(SqlSession session) {
        //关闭sqlSession
        if (session != null) {
            log.info("session commit -> session close");
//            session.commit();
            session.close();
        }
    }

}
